#include "Material.h"

namespace egg{

    Material::Material(ShaderPtr shader){
        mShader=shader;
        mFillMode=GL_FILL;
        mCullMode=GL_BACK;
        mStencilMask=SM_OPACITY;
        mTransparentType=TT_SURFACE;
        mParamMap.bindShader(mShader);
        mParamMap.bindNumericParam("uni_vec4_frustum");
        mExt=mParamMap.getNumericParam("uni_vec4_ext")->getPtr<vec4>();
        mColor=mParamMap.getNumericParam("uni_vec4_color")->getPtr<vec4>();
        setOpacity(1.0f);
        setReflection(0.0f);
        setRefraction(0.0f);
        setShininess(50.0f);
    }

    void Material::setColor(const vec4& rgba){
        mColor->r=rgba.r;
        mColor->g=rgba.g;
        mColor->b=rgba.b;
    }

    void Material::setOpacity(float opacity){
        mExt->r=opacity;
        if(opacity<1.0){
            mStencilMask|=SM_TRANSPARENT;
        }else{
            mStencilMask&=~SM_TRANSPARENT;
        }
    }

    void Material::setShininess(float shininess){
        mColor->a=shininess;
    }

    void Material::setRefraction(float refraction){
        mExt->g=refraction;
        if(refraction>0){
            mStencilMask|=SM_REFRACT;
        }else{
            mStencilMask&=~SM_REFRACT;
        }
    }

    void Material::setReflection(float reflection){
        mExt->b=reflection;
        if(reflection>0){
            mStencilMask|=SM_REFLECT;
        }else{
            mStencilMask&=~SM_REFLECT;
        }
    }

    void Material::setTransparentType(TransparentType type){
        mTransparentType=type;
    }

}